﻿// 大气监测 图形绘制统一方法
(function () {
    var instance;
    window.PollutantChartHelper = function (chartSelector, dataSource, pollutantNameObj, pollutantUnitObj, pollutantConst, chartTitle) {
        if (instance == undefined) {
            instance = this;
            this.ChartSelector = chartSelector;
            this.DataSource = dataSource;
            this.PollutantNameObj = pollutantNameObj;
            this.PollutantUnitObj = pollutantUnitObj;
            this.PollutantConst = pollutantConst;
            this.Title = chartTitle;
            this.DrawChart = function (contentType) {
                // 子标题，纵轴标识
                var subTitle = "";
                if (this.PollutantNameObj.hasOwnProperty(contentType)) {
                    subTitle += this.PollutantNameObj[contentType];
                }
                if (this.PollutantUnitObj.hasOwnProperty(contentType)) {
                    subTitle += "(" + this.PollutantUnitObj[contentType] + ")";
                }
                //横轴数组
                var categoriesArray = [];
                //数据过滤
                var seriesObject = {};

                for (var i = 0, length = this.DataSource.length; i < length; i++) {
                    var dataItem = this.DataSource[i];
                    if (categoriesArray.indexOf(dataItem["TimePoint"]) === -1) {
                        categoriesArray.push(dataItem["TimePoint"]);
                    }
                    if (!seriesObject.hasOwnProperty(dataItem["StatisticsName"])) {
                        seriesObject[dataItem["StatisticsName"]] = [];
                    }
                    if (dataItem.hasOwnProperty(contentType)) {
                        seriesObject[dataItem["StatisticsName"]].push(dataItem[contentType]);
                    } else {
                        seriesObject[dataItem["StatisticsName"]].push(null);
                    }
                }
                var seriesArray = [];
                for (var key in seriesObject) {
                    if (seriesObject.hasOwnProperty(key)) {
                        seriesArray.push({
                            type: "spline",
                            name: key,
                            data: seriesObject[key]
                        });
                    }
                }
                // 限值组
                var yPlots = [];
                if (this.PollutantConst.PollutantLevelValue.hasOwnProperty(contentType)) {
                    yPlots = this.PollutantConst.PollutantLevelValue[contentType];
                }
                // x轴间距
                var step = parseInt(categoriesArray.length / 6);
                if (0 === step) {
                    step = 1;
                }

                this.ChartSelector.highcharts({
                    lang: {
                        contextButtonTitle: '导出图片',
                        decimalPoint: '.',
                        //downloadJPEG: "下载JPEG图片",
                        //downloadPDF: "下载PDF文件",
                        //downloadPNG: "下载PNG文件",
                        //downloadSVG: "下载SVG文件",
                        loading: '加载中...',
                        noData: "没有数据",
                        //printChart: "打印图表",
                    },
                    title: {
                        text: this.Title
                    },
                    subtitle: {
                        text: subTitle
                    },
                    colors: this.PollutantConst.ColorArray,
                    exporting: {
                        buttons: {
                            contextButton: {
                                menuItems: null,
                                onclick: function () {
                                    this.exportChart();
                                }
                            }
                        },
                        sourceHeight: chartSelector.height(),
                        sourceWidth: chartSelector.width()
                    },
                    yAxis: {
                        title: {
                            text: subTitle
                        },
                        plotLines: yPlots
                    },
                    xAxis: {
                        categories: categoriesArray,
                        labels: {
                            step: step
                        }
                    },
                    credits: {
                        enabled: false
                    },
                    series: seriesArray,
                    noData: {
                        style: {
                            fontWeight: 'bold',
                            fontSize: '15px',
                            color: '#303030'
                        }
                    },
                    tooltip: {
                        shared:true
                    }
                });
            }
        }
        return instance;
    }
})();